<?php

/**
* @copyright   (C) 2010 iJoomla, Inc. - All rights reserved.
* @license  GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html) 
* @author  iJoomla.com webmaster@ijoomla.com
* @url   http://www.ijoomla.com/licensing/
* the PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript  
* are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0 
* More info at http://www.ijoomla.com/licensing/
*/

defined('_JEXEC') or die('Restricted access');

class ViewDetails{
	function ViewDetails(){
	}
	
	function edit(){
		$return  = '<table width="100%">';
		$return .= 		'<tr>';
		$return .= 			'<td>';
		$return .= 				JText::_("COM_SURVEYS_RESPONS_TEXT");
		$return .= 			'</td>';
		$return .= 		'</tr>';
		$return .= '</table>';
		$return .= '<br />';
		$return .= '<table width="100%">';
		$return .= 		'<tr>';
		$return .= 			'<td width="5%">#</td>';
		$return .= 			'<td>';
		$return .= 				'<b>'.JText::_("COM_SURVEYS_VALUE").'</b>';
		$return .= 			'</td>';
		$return .= 		'</tr>';
		
		$question_type = $this->getQuestionType();				
		$responses = $this->getResponses();		
		if(isset($responses) && is_array($responses) && count($responses) > 0){
			$i = 1;
			foreach($responses as $key=>$value){
				$return .= 		'<tr>';
				$return .= 			'<td>'.$i++.'</td>';
				$return .= 			'<td>';
				if($question_type == "13"){
					$return .= date('Y-m-d H-i-s', $value["value"]);
				}
				else{
					$return .= $value["value"];
				}
				$return .= 			'</td>';
				$return .= 		'</tr>';
			}
		}		
		$return .= '</table>';
		
		$return .= '<table width="100%">';
		$return .= 		'<tr>';
		$return .= 			'<td align="center" width="100%">';
		$return .= 				'<input type="button" onclick="javascript:history.go(-1);" value="&lt;&lt; '.JText::_("COM_SURVEYS_BACK").'">';
		$return .= 			'</td>';
		$return .= 		'</tr>';
		$return .= '</table>';
		return $return;
	}
	
	function getResponses(){
		$a_id = JRequest::getVar("a_id", "0");
		$q_id = JRequest::getVar("q_id", "0");
		$r_id = JRequest::getVar("r_id", "0");
		$and = "";
		if($r_id != "0"){
			$and .= " and session_id=".$r_id;
		}
		
		if($a_id == "0"){
			$db =& JFactory::getDBO();
			$sql = "select value from #__ijoomla_surveys_result_text where question_id=".$q_id.$and;
			$db->setQuery($sql);
			$db->query();
			$result = $db->loadAssocList();
			return $result;
		}
		else{
			$db =& JFactory::getDBO();
			$sql = "select value from #__ijoomla_surveys_result where a_id=".$a_id.$and;
			$db->setQuery($sql);
			$db->query();
			$result = $db->loadAssocList();
			return $result;
		}
	}
	
	function getQuestionType(){
		$q_id = JRequest::getVar("q_id", "0");
		$db =& JFactory::getDBO();
		$sql = "select question_type from #__ijoomla_surveys_questions where id=".$q_id;
		$db->setQuery($sql);
		$db->query();
		$result = $db->loadResult();
		return $result;
	}
};
?>